package org.jdesktop.wonderland.modules.kinectcontrol.client;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ResourceBundle;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
import org.jdesktop.mtgame.WorldManager;
import org.jdesktop.wonderland.client.BaseClientPlugin;
import org.jdesktop.wonderland.client.cell.view.ViewCell;
import org.jdesktop.wonderland.client.hud.CompassLayout;
import org.jdesktop.wonderland.client.hud.HUD;
import org.jdesktop.wonderland.client.hud.HUDComponent;
import org.jdesktop.wonderland.client.hud.HUDEvent;
import org.jdesktop.wonderland.client.hud.HUDEventListener;
import org.jdesktop.wonderland.client.hud.HUDManagerFactory;
import org.jdesktop.wonderland.client.jme.ClientContextJME;
import org.jdesktop.wonderland.client.jme.JmeClientMain;
import org.jdesktop.wonderland.client.jme.ViewManager;
import org.jdesktop.wonderland.client.login.ServerSessionManager;
import org.jdesktop.wonderland.common.annotation.Plugin;

/**
 *
 * @author Dominik
 */
@Plugin
public class KinectControlClientPlugin extends BaseClientPlugin {

    // The top map menu item to add to the Windows menu
    private JMenuItem kinectMI = null;
    // The HUD Component displaying the top map.
    private KinectControlJPanel kinectHUD = null;
    // The HUD Component displaying the top map.
    private HUDComponent hudComponent = null;
    // Listener for changes in the primary view Cell
    private ViewManager.ViewManagerListener viewManagerListener = null;
    
    private ServerSessionManager serverSession;
    
    
    /**
     * {@inheritDoc}
     */
    @Override
    public void initialize(ServerSessionManager loginInfo) {
        
        serverSession = loginInfo;
        // Create the Top Map menu item. Upon activation, create the HUD window
        // that displays the map.
        kinectMI = new JCheckBoxMenuItem("Kinect Controller");
        kinectMI.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // Depending upon whether the checkbox is selected or not,
                // either hide or show the HUD Component and turn on/off the
                // camera taking the snapshots for the map.
                if (kinectMI.isSelected() == true) {
                    if (hudComponent == null) {
                        hudComponent = createHUDComponent(serverSession);
                    }
                    hudComponent.setVisible(true);
                }
                else {
                    hudComponent.setVisible(false);
                }
            }
        });
        
        // Wait until we get a "primary view" Cell. We need one before we can
        // display the map.
        viewManagerListener = new MapViewManagerListener();
        ViewManager.getViewManager().addViewManagerListener(viewManagerListener);
        
        super.initialize(loginInfo);
    }
    
        /**
     * Creates and returns the top map HUD component.
     */
    private HUDComponent createHUDComponent(ServerSessionManager serverSession) {

        // Create the HUD Panel that displays the map.
        HUD mainHUD = HUDManagerFactory.getHUDManager().getHUD("main");
        KinectControlJPanel panel = new KinectControlJPanel(serverSession);
        hudComponent = mainHUD.createComponent(panel);
        hudComponent.setName("Kinect Controller");
        hudComponent.setPreferredLocation(CompassLayout.Layout.NORTHEAST);
        mainHUD.addComponent(hudComponent);

        // Track when the HUD Component is closed. We need to update the state
        // of the check box menu item too. We also need to turn off the camera.
        hudComponent.addEventListener(new HUDEventListener() {
            public void HUDObjectChanged(HUDEvent event) {
                if (event.getEventType() == HUDEvent.HUDEventType.CLOSED) {
                    kinectMI.setSelected(false);
                }
            }
        });
        
        return hudComponent;
    }  
    
    /**
     * {@inheritDoc}
     */
    @Override
    public void cleanup() {
        // If there is a HUD Component, then remove it from the HUD and clean
        // it up. This really should happen when the primary view cell is
        // disconnected, but we do this here just in case.
        if (hudComponent != null) {
            HUD mainHUD = HUDManagerFactory.getHUDManager().getHUD("main");
            mainHUD.removeComponent(hudComponent);
        }
        
    }
    
        /**
     * Listener for the changes in the primary view cell.
     */
    private class MapViewManagerListener implements ViewManager.ViewManagerListener {
        /**
         * {@inheritDoc}
         */
        public void primaryViewCellChanged(ViewCell oldCell, ViewCell newCell) {
            
            // If there is an old Cell, then remove the old HUD component and
            // the main menu item.
            if (oldCell != null) {
                // If there is a HUD Component, then remove it from the HUD and
                // clean it up. This really should happen when the primary view
                // cell is disconnected, but we do this here just in case.
                if (hudComponent != null) {
                    HUD mainHUD = HUDManagerFactory.getHUDManager().getHUD("main");
                    mainHUD.removeComponent(hudComponent);
                }

                // Remove the menu item
                JmeClientMain.getFrame().removeFromWindowMenu(kinectMI);
            }

            // If there is a new Cell, the add the menu item
            if (newCell != null) {
                JmeClientMain.getFrame().addToWindowMenu(kinectMI, -1);
            }
        }
    }
}
